/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.GoodsType;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.service.GoodsTypeService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.pay.wxpay.api.UUID;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztGoodsHand")
public class ZtGoodsHandController extends BaseController {

	@Autowired
	private ZtGoodsService ztGoodsService;
	
	@Autowired
	private GoodsTypeService goodsTypeService;
	
	@Autowired	
	private UserService userService;
	
	@Autowired  
    private MyProps myProps;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtGoods get(String id, boolean isNewRecord) {
		return ztGoodsService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztGoodsHand:ztGoodsDataHand:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtGoods ztGoods, Model model) {
		model.addAttribute("ztGoods", ztGoods);
		List<GoodsType> typeList=goodsTypeService.findList(new GoodsType());
		model.addAttribute("typeList", typeList);
		
		return "modules/ztGoods/ztGoodsHandList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztGoodsHand:ztGoodsDataHand:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtGoods> listData(ZtGoods ztGoods, HttpServletRequest request,
			HttpServletResponse response,Model model) {
		ztGoods.setGoodstemp("2");//二手
		Page<ZtGoods> page = ztGoodsService.findPage(new Page<ZtGoods>(request,
				response), ztGoods);
		
		String pathPx=myProps.getServerPath();
		for(ZtGoods zd:page.getList()){
			String img="";
			try{
			 img=myProps.getServerPath()+ztGoodsService.getImgFirst(zd);
			}catch(Exception ex){
				img="";
			}
			zd.setImg(img);
		}
		
		return page;
	}
	
	
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztGoodsHand:ztGoodsDataHand:view")
	@RequestMapping(value = "form")
	public String form(ZtGoods ztGoods, Model model,HttpServletRequest request,HttpServletResponse response) {
		if(ztGoods!=null&&StringUtils.isBlank(ztGoods.getId())){
			ztGoods.setZtGoodsid(String.valueOf(UUID.next()));
		}
		model.addAttribute("ztGoods", ztGoods);
		List<GoodsType> typeList=goodsTypeService.findList(new GoodsType());
		model.addAttribute("typeList", typeList);
		
		User user=new User();
		user.setMgrType(User.MGR_TYPE_CORP_ADMIN);	// 租户管理员
		// 禁用自动添加租户代码条件，添加自定义租户查询条件
		user.getSqlMap().getWhere().disableAutoAddCorpCodeWhere()
			.and("corp_code", QueryType.EQ, user.getCorpCode_())
			.and("corp_name", QueryType.LIKE, user.getCorpName_());
		
		List<User> userList = userService.findList(user);
			//	userList
		model.addAttribute("userList", userList);
		
		return "modules/ztGoods/ztGoodsHandForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztGoodsHand:ztGoodsDataHand:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtGoods ztGoods) {
		ztGoods.setGoodstemp("2");
		ztGoodsService.save(ztGoods);
		return renderResult(Global.TRUE, text("保存商品成功！"));
	}
	
	/**
	 * 下架
	 */
	@RequiresPermissions("ztGoodsHand:ztGoodsDataHand:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ZtGoods ztGoods) {
		ztGoods.setSjStatus("2");
		ztGoodsService.updateSjStatus(ztGoods);
		return renderResult(Global.TRUE, text("下架商品成功"));
	}
	
	/**
	 * 上架
	 */
	@RequiresPermissions("ztGoodsHand:ztGoodsDataHand:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ZtGoods ztGoods) {
		ztGoods.setSjStatus("1");
		ztGoodsService.updateSjStatus(ztGoods);
		return renderResult(Global.TRUE, text("上架商品成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztGoodsHand:ztGoodsDataHand:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtGoods ztGoods) {
		ztGoodsService.delete(ztGoods);
		return renderResult(Global.TRUE, text("删除商品成功！"));
	}
}